package com.audaque.springboot.foshanupload.jwtvalid.filter;


import com.audaque.springboot.foshanupload.jwtvalid.component.JwtLoginProvider;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * jwt登录：解析到token里的用户信息，转传到请求域
 */

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
@Component // 将Filter交给spring管理，目录是使@autowired生效
public class JwtBodyFilter implements Filter {


    @Autowired
    private JwtLoginProvider jwtLoginProvider;

    private  String[] excludedUris;


    public void init(FilterConfig filterConfig) throws ServletException {
        String param = filterConfig.getInitParameter("excludedUris");//获取参数值
        if (StringUtils.isNotBlank(param)) {
            this.excludedUris = param.split(",");
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        jwtLoginProvider.login(httpServletRequest,httpServletResponse);
        filterChain.doFilter(httpServletRequest, httpServletResponse);
        return;
    }


}
